package cn.com.twoke.easyproject.config.thread_pool;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadFactory;

@Slf4j
public class DealErrorThreadFactory implements ThreadFactory {

    private final ThreadPoolTaskExecutor  executor;

    public DealErrorThreadFactory(ThreadPoolTaskExecutor executor) {
        this.executor = executor;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = this.executor.createThread(r);
        thread.setUncaughtExceptionHandler((t,e) -> {
            log.error(e.getMessage(),  e);
        });
        return thread;
    }

}
